const callbackEvent: any = {};

const on = (key: string, callback: Callback) => {
  const callbackList = (callbackEvent[key] || []) as Callback[];
  callbackList.push(callback);
  callbackEvent[key] = callbackList;
};

const off = (key: string, callback: Callback) => {
  const callbackList = (callbackEvent[key] || []) as Callback[];
  callbackList.splice(
    callbackList.findIndex((i) => i === callback),
    1
  );
};

const emit = (key: string, data?: any) => {
  const callbackList = (callbackEvent[key] || []) as Callback[];
  callbackList.forEach((i) => i(data));
};

export default { on, off, emit };

type Callback = (data?: any) => void;
